<?php
namespace App\Http\Controllers\ConsoleWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\App\PublicBusiness;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use App\Http\Controllers\Business\App\Salesman\SalesmanManagement;
use App\Models\FirstClass;
use App\Models\Order;
use App\Models\RedEnvelope;
use App\Models\SalesmanInfo;
use App\Models\SalesmanRegister;
use App\Models\SecondClass;
use App\Models\ShopAccount;
use App\Models\ShopContract;
use App\Models\ShopReview;
use App\Models\Province;
use App\Models\City;
use App\Models\AgentWithdrawal;
use App\Models\AdminUser;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\User;
use App\Models\Shop;
use App\Models\ShopUser;
use App\Models\District;
use App\Models\BusinessArea;
use App\Http\Controllers\Business\Admin\Salesman\ShopSalesmanManagement;
use App\Utils\ChuangLanVoiceCodeByV2Version;
use SimpleSoftwareIO\QrCode\Facades\QrCode;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use ZipArchive;
use Illuminate\Support\Facades\DB;

class TestController extends WebController
{
    public function test() {
        if($this->request->get('type') == 'shopid') {
            //shopuser 新增shopid
            if ($this->request->has('page')) {
                $page = $this->request->get('page');
            } else {
                $page = 1;
            }
            $length = 200;
            $skip = ($page - 1) * $length;
            $shopUsers = ShopUser::where('shopid', NULL)->where('auth', '<>', '')->skip($skip)->take($length)->get();
            $count = 0;
            foreach ($shopUsers as $shopUser) {
                $poolIds = '';
                $authJson = $shopUser->auth;
                $authArray = json_decode($authJson, true);
                if (!empty($authArray)) {
                    foreach ($authArray as $k => $v) {
                        $poolIds .= $k . ',';
                    }
                    $poolIds = rtrim($poolIds, ",");
                    $shopUser->shopid = $poolIds;
                    $shopUser->save();
                    ++$count;
                }
            }
            echo $count;return;
        }
        elseif($this->request->get('type') == 'log') {
            $fileName= "../storage/logs/laravel.log";
            if($this->request->get('isdelfile') == 1) {
                return response()->download($fileName)->deleteFileAfterSend(true);
            } else {
                return response()->download($fileName);
            }
        }
        elseif($this->request->get('type') == 'city') {
            if(City::where('id', '<>', 1)->where('validdays', 7)->get()->count()>0) {
                echo "已经更新了";
            }
            City::where('id', '<>', 1)->where('validdays', "<>", 7)->update(['validdays'=>7]);
            echo "更新成功了";
        }
        elseif($this->request->get('type') == 'salesman_register') {
            if(SalesmanRegister::where('registermobile', '13043002433')->where('isvalid', 1)->count()>0) {
                echo "已经更新了";
            }
            $mobileArray = [
                '13328780510', '18205981660', '15759247515', '18695697734', '18350258435', '13067161729', '18650101055', '15960825202',
                '13860173198', '13600920951', '13030881558', '13023917563', '13950025033', '18059552332', '15959551025', '18859212302', '15958837531',
                '18030183621', '13015918596', '13799272411', '15980811385', '18205913027', '18659548642', '18750215035', '15980968549', '13600956807', '15260646080',
                '17828089001', '18059221037', '18650166767', '15710678135', '18350618973', '15659835490', '18805071762', '13959299047', '13600940163',
                '13043002433', '13101433398', '15960207323', '15259250380', '13696907165', '15359401503', '15711527233', '18359285922', '13275040050',
                '15606953871', '13159207076', '15985843432', '18720433550', '13606042416', '13459009489', '18965101189', '15960292685', '13459001238',
                '15960276400', '13906013843', '13695040848', '15710675021', '13105006900', '18259484543', '15259249875', '15985893265', '13751334952', '15980961818',
                '18350230138', '15084486664', '18959242388', '18965832937', '13023901746', '15759250724', '18650021663', '15980947964', '15985881213'
            ];
            $i=0;
            foreach($mobileArray as $list) {
                SalesmanRegister::where('registermobile', $list)->where('isvalid', 0)->update(['isvalid'=>1]);
                ++$i;
            }
            echo "已经更新了" . $i . "个" ;
        }
        elseif($this->request->get('type') == 'user') {
            set_time_limit(0);
            $time = $this->request->get('time');
            if(!$time)
            {exit;}
            $length = $this->request->get('length');
            $mobile = ["1300620","1300621","1300720","1300721","1300722","1300723","1300724","1300728","1301072","1302620","1302621","1302720","1302721","1302722","1302723","1302724","1302728","1303620","1303621","1303720","1303721","1303722","1303723","1303724","1303728","1304490","1304491","1304790","1304791","1306410","1306411","1306412","1306413","1306414","1306510","1306511","1306512","1306513","1306514","1306515","1306516","1306517","1306518","1306519","1307790","1307791","1307792","1307793","1307794","1307795","1307796","1307797","1307798","1307799","1309720","1309721","1309728","1311780","1311781","1311791","1312521","1313380","1313381","1313382","1313383","1313384","1313391","1315580","1315581","1315582","1315583","1317086","1317087","1317088","1317091","1317777","1317778","1317780","1317781","1317782","1317783","1317784","1317785","1317786","1317787","1317788","1317789","1317791","1318456","1318457","1318458","1318459","1319791","1320700","1320708","1320709","1320791","1321700","1321791","1323700","1323704","1323708","1323751","1323752","1323764","1323791","1324700","1324708","1324709","1324770","1324771","1324776","1324778","1324780","1324781","1324784","1324786","1324791","1325700","1325708","1325709","1325791","1326300","1326309","1326391","1330700","1330708","1330709","1330791","1331700","1331705","1331791","1331940","1331941","1331942","1331943","1332001","1332011","1333005","1333006","1333007","1333008","1333009","1333010","1333011","1333012","1334001","1334011","1336161","1336162","1336163","1336164","1336165","1336171","1336700","1336708","1336709","1336791","1338700","1338708","1338709","1338791","1339700","1339708","1339709","1339791","1340700","1340708","1340709","1340791","1342668","1343700","1343708","1343709","1343790","1343791","1347910","1347911","1347912","1347913","1347914","1347915","1347916","1347917","1347918","1347919","1350700","1350708","1350709","1350791","1351700","1351708","1351709","1351791","1357600","1357601","1357602","1357603","1357604","1357605","1357606","1357607","1357608","1357609","1357610","1357611","1357612","1357613","1357614","1357625","1357626","1357627","1357628","1357629","1357690","1357691","1357692","1357693","1357694","1357695","1357696","1357697","1357698","1357699","1360700","1360704","1360706","1360708","1360709","1360791","1361700","1361708","1361709","1361791","1362700","1362708","1362709","1362791","1364700","1364708","1364709","1364791","1365700","1365708","1365709","1365791","1366700","1366708","1366709","1366791","1367220","1367221","1367222","1367223","1367224","1367700","1367708","1367709","1367791","1368481","1368700","1368708","1368709","1368791","1369488","1369700","1369708","1369709","1369791","1369800","1369809","1369841","1369950","1369951","1369952","1369953","1369954","1369955","1369956","1370708","1370709","1370791","1373290","1373291","1373292","1373293","1373294","1373295","1373296","1373297","1373298","1373299","1374070","1374079","1374270","1374279","1375560","1375561","1375562","1375563","1375564","1375565","1375566","1375567","1375568","1375569","1375575","1375576","1375577","1375578","1375579","1376700","1376701","1376702","1376703","1376704","1376705","1376706","1376707","1376708","1376709","1376710","1376711","1376712","1376713","1376714","1376715","1376716","1376717","1376718","1376719","1376740","1376741","1376742","1376743","1376744","1376745","1376746","1376747","1376748","1376749","1376795","1376796","1376797","1376798","1376799","1380350","1380351","1380352","1380353","1380354","1380700","1380703","1380704","1380705","1380706","1380708","1380709","1380791","1387005","1387006","1387007","1387008","1387009","1387060","1387061","1387062","1387063","1387064","1387065","1387066","1387067","1387068","1387069","1387080","1387081","1387082","1387083","1387084","1387085","1387086","1387087","1387088","1387089","1387090","1387091","1387092","1387093","1387094","1387095","1387096","1387097","1387098","1387099","1387910","1387911","1387912","1387913","1387914","1387915","1387916","1387917","1387918","1387919","1390070","1390079","1390700","1390708","1390709","1390791","1397000","1397001","1397002","1397003","1397004","1397005","1397006","1397007","1397008","1397009","1397080","1397081","1397082","1397083","1397084","1397085","1397086","1397087","1397088","1397089","1397090","1397091","1397092","1397093","1397094","1397095","1397096","1397097","1397098","1397099","1397910","1397911","1397912","1397913","1397914","1397915","1397916","1397917","1397918","1397919","1500700","1500791","1507000","1507001","1507002","1507003","1507004","1507005","1507006","1507007","1507008","1507009","1507040","1507041","1507080","1507081","1507082","1507083","1507084","1507085","1507086","1507087","1507088","1507089","1507090","1507091","1507092","1507093","1507094","1507095","1507096","1507097","1507098","1507099","1507900","1507901","1507902","1507903","1507904","1507905","1507906","1507907","1507908","1507909","1507910","1507911","1507912","1507913","1507914","1507915","1507916","1507917","1507918","1507919","1508350","1508351","1508352","1508353","1508354","1508355","1508380","1508381","1508382","1508383","1508384","1510791","1517000","1517001","1517002","1517003","1517004","1517005","1517006","1517007","1517008","1517009","1517020","1517021","1517022","1517023","1517024","1517040","1517041","1517042","1517043","1517044","1517045","1517046","1517047","1517048","1517049","1517910","1517911","1517912","1517913","1517914","1517915","1517916","1517917","1517918","1517919","1518010","1518011","1518012","1518013","1518014","1518015","1518016","1518017","1518018","1518019","1518040","1518041","1518042","1518043","1518044","1518045","1518046","1518047","1518048","1518049","1527097","1527094","1527087","1527095","1530700","1530791","1533775","1534791","1550791","1557910","1557911","1557912","1557913","1557914","1557915","1557916","1557917","1557918","1560791","1567910","1567911","1567912","1567913","1567914","1567915","1567916","1567917","1567918","1567919","1570791","1571791","1572763","1572764","1572765","1572767","1572767","1572768","1571709","1580700","1580791","1587000","1587001","1587002","1587003","1587060","1587061","1587062","1587063","1587064","1587065","1587066","1587067","1587068","1587069","1587900","1587901","1587902","1587903","1587904","1587905","1587906","1587907","1587908","1587909","1587910","1587911","1587912","1587913","1587914","1587915","1587916","1587917","1587918","1587919","1590700","1590708","1590709","1590791","1597040","1597041","1597042","1597043","1597044","1597045","1597046","1597047","1597048","1597049","1597060","1597061","1597062","1597063","1597064","1597065","1597066","1597067","1597068","1597069","1597900","1597901","1597902","1597903","1597904","1597905","1597906","1597907","1597908","1597909","1597910","1597911","1597912","1597913","1597914","1597915","1597916","1597917","1597918","1597919","1880708","1887007","1897090","1897914"];
            for($i = 0;$i < $length; $i++) {
                try {
                    $rand1 = rand(0,9);
                    if($rand1 < 1) {
                        $timerand = strtotime($time) + rand(28800,36000);
                    } elseif ($rand1 > 8) {
                        $timerand = strtotime($time) + rand(72000,79200);
                    } else {
                        $timerand = strtotime($time) + rand(36000,72000);
                    }
                    $user = new User();
                    $user->mobile = $mobile[rand(0,count($mobile) - 1)] . rand(1000,9999);
                    $user->nickname = 'fbw_' . $timerand;
                    $user->avatar = 'http://img.fbw-china.com/images/default_avatar.png';
                    $user->created_at = date('Y-m-d H:i:s',$timerand);
                    $user->save();
                } catch (\Exception $e) {
                    continue;
                }
            }
            echo "更新成功";
        }
        elseif($this->request->get('type') == 'shop_oldid') {
            $page = $this->request->get('page');
            $length = 100;
            $skip = ($page - 1) * $length;
            $shops = Shop::where('status', 1)->skip($skip)->take($length)->get();
            $i = 0;
            foreach($shops as $shop) {
                $shopId = $shop['id'];
                $shopContractor = ShopContract::where('shopid', $shopId)->orderBy('id', 'desc')->first();
                if($shopContractor['contractorid']) {
                    $shop->oldid = $shopContractor['contractorid'];
                    $shop->oldmaintainer = AdminUser::where('id', $shopContractor['contractorid'])->pluck('realname')->first();
                    $shop->save();
                    $i++;
                }
            }
            echo "更新了" . $i . "个";
        }
    }
    //新增晋城
    public function addJinCheng() {
        if(City::where('name', '晋城市')->get()->count()>0) {
            echo "已经更新了";
            return;
        }
        $province = Province::where('name', '山西省')->first();
        if(!$province) {
            $province = Province::create([
                'name' => '山西省',
                'letter' => 'S',
                'status' => 1
            ]);
            $province->save();
        }
        $provinceId = $province->id;
        $city = City::create([
            'name' => '晋城市',
            'provinceid' => $provinceId,
            'letter' => 'J',
            'status' => '1',
        ]);
        $city->save();
        $cityId = $city->id;
        //高平市：东城街道  河西镇 北城街道 米山镇 寺庄镇 南城街道 马村镇 原村乡 神农镇 三甲镇 永录乡
        $district1 = District::create([
            'name' => '高平市',
            'status' => 1,
            'letter' => 'G',
            'cityid' => $cityId
        ]);
        $district1->save();
        $districtId1 = $district1->id;
        $areaArray1 = ["东城街道",  "河西镇", "北城街道", "米山镇", "寺庄镇", "南城街道", "马村镇", "原村乡", "神农镇", "三甲镇", "永录乡"];
        foreach($areaArray1 as $area) {
            $businessArea = BusinessArea::create([
                'name' => $area,
                'letter' => 'D',
                'status' => 1,
                'districtid' => $districtId1
            ]);
            $businessArea->save();
        }
        //阳城县：凤城镇 北留镇 润城镇 蟒河镇 横河镇 演礼乡 西河乡 董封乡
        $district2 = District::create([
            'name' => '阳城县',
            'status' => 1,
            'letter' => 'Y',
            'cityid' => $cityId
        ]);
        $district2->save();
        $districtId2 = $district2->id;
        $areaArray2 = ["凤城镇", "北留镇", "润城镇", "蟒河镇", "横河镇", "演礼乡", "西河乡", "董封乡"];
        foreach($areaArray2 as $area) {
            $businessArea = BusinessArea::create([
                'name' => $area,
                'letter' => 'F',
                'status' => 1,
                'districtid' => $districtId2
            ]);
            $businessArea->save();
        }
        //泽州县：金村镇 巴公镇 下村镇 南村镇 大阳镇大东沟镇川底乡 周村镇 北义城镇
        $district3 = District::create([
            'name' => '泽州县',
            'status' => 1,
            'letter' => 'Y',
            'cityid' => $cityId
        ]);
        $district3->save();
        $districtId3 = $district3->id;
        $areaArray3 = ["金村镇", "巴公镇", "下村镇", "南村镇", "大阳镇大东沟镇川底乡", "周村镇", "北义城镇"];
        foreach($areaArray3 as $area) {
            $businessArea = BusinessArea::create([
                'name' => $area,
                'letter' => 'F',
                'status' => 1,
                'districtid' => $districtId3
            ]);
            $businessArea->save();
        }
        //沁水县：端氏镇 龙港镇 嘉峰镇 郑村镇 固县乡
        $district4 = District::create([
            'name' => '沁水县',
            'status' => 1,
            'letter' => 'Y',
            'cityid' => $cityId
        ]);
        $district4->save();
        $districtId4 = $district4->id;
        $areaArray4 = ["端氏镇", "龙港镇", "嘉峰镇", "郑村镇", "固县乡"];
        foreach($areaArray4 as $area) {
            $businessArea = BusinessArea::create([
                'name' => $area,
                'letter' => 'F',
                'status' => 1,
                'districtid' => $districtId4
            ]);
            $businessArea->save();
        }
        //陵川县：崇文镇 礼义镇 附城镇 夺火乡 马圪当乡 古郊乡
        $district4 = District::create([
            'name' => '陵川县',
            'status' => 1,
            'letter' => 'Y',
            'cityid' => $cityId
        ]);
        $district4->save();
        $districtId4 = $district4->id;
        $areaArray4 = ["崇文镇", "礼义镇", "附城镇", "夺火乡", "马圪当乡", "古郊乡"];
        foreach($areaArray4 as $area) {
            $businessArea = BusinessArea::create([
                'name' => $area,
                'letter' => 'F',
                'status' => 1,
                'districtid' => $districtId4
            ]);
            $businessArea->save();
        }
    }

    public function zipFile() {
        $filename='test.zip'; //最终生成的文件名（含路径）
        if(file_exists($filename)){
            unlink($filename);
        }
        //重新生成文件
        $zip = new ZipArchive();
        if($zip->open($filename,ZIPARCHIVE::CREATE)!==TRUE){
            exit('无法打开文件，或者文件创建失败');
        }
        $file1 = 'shopController.php';
        if (!file_exists($file1)) {
            fopen($file1, 'a+');
            //chmod($file1, 0777);
        }
        $file2 = 'OrderController.php';
        if (!file_exists($file2)) {
            fopen($file2, 'a+');
            //chmod($file1, 0777);
        }
        $datalist=array($file1,$file2);
        foreach($datalist as $val){
            if(file_exists($val)){
                $zip->addFile($val);
            }
        }
        unlink($file1);
        unlink($file2);
        $zip->close();//关闭
        if(!file_exists($filename)){
            exit('无法找到文件'); //即使创建，仍有可能失败
        }
        return response()->download($filename);
    }
    public function qrcodeDownload() {
        $fileName = "夏艳.png";
        $GLOBALS['Url'] = 'https://www.fbw-china.com/';
        return response()->stream(function(){
            echo $qrc = QrCode::format('png')->size(200)->generate($GLOBALS['Url']);
        },200,["Content-type"=>"image/png","Content-Disposition"=>"attachment; filename=".$fileName]);
    }
}